# encoding:utf8
import datetime
import time
def timestamp_to_datetime(timestamp:int,dt_format:str="%Y-%m-%d %H:%M:%S")->str:
    """
    时间戳格式化
    :param timestamp: 时间戳
    :param dt_format: 格式化字符串
    :return:
    """
    return time.strftime(dt_format,time.localtime(timestamp))

def datetime_to_timestamp(datetime_strftime:str)->int:
    st = datetime.datetime.strptime(datetime_strftime,"%Y-%m-%d %H:%M:%S")
    return int(st.timestamp())

def iso_to_datetime(isotime:str,dt_format:str="%Y-%m-%d %H:%M:%S")->str:
    """
    iso时间转换为2024-01-01 00:00:00格式
    :param isotime: iso时间
    :param dt_format: 格式化字符串
    :return:
    """
    st = datetime.datetime.strptime(isotime,"%Y-%m-%dT%H:%M:%S%z")
    timestamp = int(time.mktime(st.timetuple()))
    datetime_str = time.strftime(dt_format,time.localtime(timestamp))
    return datetime_str

def parse_datetime(datetime_strftime:str=None)->dict:
    """
    解析
    :param datetime_strftime: 时间 2024-01-01 00:00:00
    :return:
    """
    date_dict = {}
    if datetime_strftime:
        datetime_strptime = datetime.datetime.strptime(datetime_strftime,"%Y-%m-%d %H:%M:%S")
    else:
        now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        datetime_strptime = datetime.datetime.strptime(now_time,"%Y-%m-%d %H:%M:%S")

    date_dict['year'] = datetime_strptime.year
    date_dict['month'] = datetime_strptime.month
    date_dict['day'] = datetime_strptime.day
    date_dict['hour'] = datetime_strptime.hour
    date_dict['minute'] = datetime_strptime.minute
    date_dict['second'] = datetime_strptime.second
    return date_dict

